<template>
  <div class="good-article">
    <van-icon
        :color="good==1?'#fb7107':'#777'"
        :name="good==1?'good-job':'good-job-o'"
        @click="onGood"
      />
  </div>
</template>
<script>
import {addGoodAPI,deleteGoodAPI} from '@/api';
export default {
  name:"GoodArticle",
  components:{},
  model:{
    prop:'good',
    event:'updataGood'
  },
  props:{
    good:{
      type:Number,
      required:true
    },
    articleId:{
      type:[Number,String,Object],
      required:true
    }
  },
  data(){
    return {}
  },
  computed:{},
  watch:{},
  methods:{
    async onGood(){
      try{
        let status=-1
        if(this.good==1){
          //已点赞  则取消点赞
          await deleteGoodAPI(this.articleId)
        }else{
          //未点赞  则添加点赞
          await addGoodAPI(this.articleId)
          status=1
        }
        //更新数据
        this.$emit('updataGood',status)
        this.$toast.success(this.good==1?"取消点赞":"点赞成功")
      }catch(err){
        this.$toast.fail("操作失败,请稍后重试!")
      }
    }
  }
}
</script>
<style lang="less" scoped>

</style>